<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class administrator extends CI_Controller { 
    public function index(){    
        $this->load->view('adminpass');     
    }
    public function admindash(){
        $this->load->view('administrator');
    }
    public function login(){
        $mobilenumber = mysql_escape_string($this->input->post('mobilenumber'));
        $adminkey = mysql_escape_string($this->input->post('adminkey'));
        $password = mysql_escape_string($this->input->post('password'));
        if (substr($mobilenumber,0,1) == '0' && is_numeric($mobilenumber)){
            $mobilenumber = "+62" . substr($mobilenumber,1);
            $password = hash('md5', $password) ;
        } 
        $query = $this->db->query('SELECT * FROM RETAILER WHERE PB_MOBILEPHONE=? AND PB_PASSKEY=? AND PB_IDRETAILER=?', array($mobilenumber,$password,$adminkey));
        if ($query->num_rows() == 1){
            $result = $query->row(); 
            $record = array(
                        'mobilephone' => $mobilenumber,
                        'email' => $result->PB_EMAILRETAILER,
                        'nama' => $result->PB_NAMARETAILER, 
                        'sessionid' => uniqid(),
                        'status' => 'logged_in'
            );
            $this->session->set_userdata($record);
            $uniqid = uniqid('pb');
            $query = $this->db->query('INSERT INTO LOG_RETAILER VALUES(?,?,NOW(),\'SUCCESS\')',array($uniqid,$mobilenumber));
            redirect('/administrator/admindash'); 
        }
        else{
            $this->session->set_flashdata('message', 'Sorry, your cant login. Please verified your identity');
            redirect('/administrator','refresh');            
        }
    }
    public function goout(){ 
        $this->session->unset_userdata('$record');
        $this->session->set_flashdata('message', 'yout have been logout');
        redirect('/administrator/index','refresh');  
    }
    public function user(){
        $query = $this->db->query('SELECT * FROM PENGGUNA');
        if($query){
            $data['daftaruser'] = $query->result(); 
            $this->load->view('user',$data);           
        }
        else if(!$query){
            $this->load->view('user');
        }
    }
    public function able(){
        $mobilephone = $this->input->post('idpengguna');
        $query = $this->db->query('SELECT * FROM PENGGUNA WHERE PB_NOHPPENGGUNA=?',array($mobilephone));
        if ($query->num_rows() == 1){
            $result = $query->row(); 
            if($result->PB_STATUS == 'ENABLED'){
                $thisquery= $this->db->query('UPDATE PENGGUNA SET PB_STATUS=\'DISABLED\' WHERE PB_NOHPPENGGUNA=?',array($mobilephone));
                $this->session->set_flashdata('message', 'disabling success');
            }
            else if($result->PB_STATUS == 'DISABLED'){
                $thisquery= $this->db->query('UPDATE PENGGUNA SET PB_STATUS=\'ENABLED\' WHERE PB_NOHPPENGGUNA=?',array($mobilephone));
                $this->session->set_flashdata('message', 'enabling success');
            }  
            redirect('/administrator/user','refresh'); 
        }
        else{
            $this->session->set_flashdata('message', 'process failed');
            redirect('/administrator/user','refresh');              
        }
    }
    public function verify($mobilephone=FALSE){
        if(!$mobilephone){
            $this->session->set_flashdata('message', 'process failed');
            redirect('/administrator/user','refresh');              
        }
        else{
            $query = $this->db->query('SELECT * FROM PENGGUNA WHERE PB_NOHPPENGGUNA=?',array($mobilephone));
            if ($query->num_rows() == 1){
                $result = $query->row(); 
                if($result->PB_STATUSPENGGUNA=='NOT VERIFIED'){
                    $thisquery= $this->db->query('UPDATE PENGGUNA SET PB_STATUSPENGGUNA=\'VERIFIED\' WHERE PB_NOHPPENGGUNA=?',array($mobilephone));
                    $this->session->set_flashdata('message', 'process success');
                }
                else{
                    $this->session->set_flashdata('message', 'process failed');
                }
                redirect('/administrator/user','refresh');
            }
            else{
                $this->session->set_flashdata('message', 'process failed');
                redirect('/administrator/user','refresh');                  
            }
        }
    }
    public function reset($mobilephone=FALSE){
        if(!$mobilephone){
            $this->session->set_flashdata('message', 'process failed');
            redirect('/administrator/user','refresh');              
        }
        else{
            $query = $this->db->query('SELECT * FROM PENGGUNA WHERE PB_NOHPPENGGUNA=?',array($mobilephone));
            if ($query->num_rows() == 1){
                $result = $query->row(); 
                $pass = uniqid('new');
                $newpassword = hash('md5', $pass);
                $thisquery= $this->db->query('UPDATE PENGGUNA SET PB_PASSWORD=? WHERE PB_NOHPPENGGUNA=?',array($newpassword,$mobilephone));
                $this->session->set_flashdata('message', 'process success'); 
                redirect('/administrator/user','refresh'); 
            }
            else{
                $this->session->set_flashdata('message', 'process failed');
                redirect('/administrator/user','refresh');                  
            }            
        }
    }
    public function delete(){
        $mobilephone = $this->input->post('idpengguna');
        $query = $this->db->query('SELECT * FROM PENGGUNA WHERE PB_NOHPPENGGUNA=?',array($mobilephone));
        if ($query->num_rows() == 1){
            $result = $query->row();  
            $thisquery= $this->db->query('UPDATE PENGGUNA SET PB_STATUS=\'BLOCKED\' WHERE PB_NOHPPENGGUNA=?',array($mobilephone));
            $this->session->set_flashdata('message', 'deleting success');  
            redirect('/administrator/user','refresh'); 
        }
        else{
            $this->session->set_flashdata('message', 'process failed');
            redirect('/administrator/user','refresh');              
        }
    }
    public function order(){
        $query = $this->db->query('SELECT * FROM DETILTRANSAKSI');
        if($query){
            $data['daftarorder'] = $query->result(); 
            $this->load->view('adorder',$data);           
        }
        else if(!$query){
            $this->load->view('adorder');
        }
    } 
}
?>
